// pages/login/empower/empower.js
const app = getApp()
const Ajax = require('request/api/index')


Page({
  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    sourceUrl: app.sourceUrl,
    fileHost: app.fileHost,
    canIUseGetUserProfile: false,
    defaultAvatarUrl: 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
    avatarUrl: '',
    phoneNoCode: '',
    avatarOpen: false,
    code: '',
    sessionId: ''
  },

  formsubmit(e) {
    const nickName = e.detail.value.nickname
    let that = this
    if (!this.data.avatarUrl) {
      wx.showToast({
        title: '请选择头像',
        icon: 'error'
      })
      return
    }
    if (!nickName) {
      wx.showToast({
        title: '请填写昵称',
        icon: 'error'
      })
      return
    }
    wx.showLoading({
      title: '上传中',
      mask: true
    })
    wx.getImageInfo({
      src: this.data.avatarUrl,
      success: (res) => {
        console.log(res)
        // app.request({
        //     api: 'applet-file-uploadSingle',
        //     filePath: res.path,
        //     filePathName: 'file',
        //     success: (res) => {
        //         console.log(res.name)
        //         wx.hideLoading()
        //         app.request({
        //             api: 'applet-wx-registry',
        //             data: {
        //                 userInfo: {
        //                     nickName: nickName,
        //                     avatarUrl: res.name
        //                 },
        //                 code: that.data.phoneNoCode,
        //                 sessionId: that.data.sessionId
        //             },
        //             success: (res) => {
        //                 wx.setStorageSync('token', res.access_token)
        //                 app.globalData.token = res.access_token
        //                 app.globalData.loginState = true
        //                 app.getUserInfo()
        //                 wx.navigateBack({
        //                     detail: 1
        //                 })
        //             }
        //         })
        //     },
        //     error: (err) => {
        //         wx.hideLoading()
        //         wx.showToast({
        //             title: '上传失败，请稍后重试',
        //             icon: 'error'
        //         })
        //     }
        // })
      },
      fail: (err) => {
        console.log(err)
      }
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.setData({
      sessionId: app.globalData.sessionId
    })

  },
  getPhoneNumber(e) {
    if (e.detail.errMsg == 'getPhoneNumber:ok') {
      wx.login({
        success: res => {
          app.globalData.code = res.code
          console.log(res.code)
          // this.setData({
          //   code: res.code
          // })
          Ajax.login({
            phoneCode: e.detail.code || e.detail.encryptedData,
            loginCode: res.code,
            state: 'default'
          }).then(res => {
            console.log(res.data)
            for (let k in res.data) {
              app.globalData[k] = res.data[k]
            }
            wx.setStorage({
              key:"token",
              data: JSON.stringify(res.data)
            })
            app.resetUserInfo(() => {
              wx.switchTab({
                url: '/pages/home/home'
              })
            })
          })
        },
        fail: () => {
          wx.showModal({
            title: '提示',
            content: '登录失败，请检查您的网络',
            showCancel: false
          })
          wx.hideLoading()
        }
      })
      // this.setData({
      //     phoneNoCode: e.detail.code || e.detail.encryptedData,
      //     // avatarOpen: true
      // })
    } else {
      //用户按了拒绝按钮
      this.empowerNot()
    }
  },

  onChooseAvatar(e) {
    const {
      avatarUrl
    } = e.detail
    console.log(avatarUrl)
    this.setData({
      avatarUrl,
    })
  },

  empowerNot() {
    wx.showModal({
      title: '警告',
      content: '您点击了拒绝授权，将无法进入小程序，请授权之后再进入!!!',
      showCancel: false,
      confirmText: '返回授权',
      success: function (res) {
        // 用户没有授权成功，不需要改变 isHide 的值
        if (res.confirm) {
          console.log('用户点击了“返回授权”');
        }
      }
    });
  },
  nolog() {
    wx.navigateBack({
      delta: 1
    })
  },
  register: (fromData) => {

  }
})